package com.zj.syzs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.zj.shouyou.R;
import com.zj.syzs.fragment.ActiveFragment;
import com.zj.syzs.fragment.GiftFragment;
import com.zj.syzs.fragment.MineFragment;
import com.zj.syzs.fragment.MoneyFragment;
import com.zj.syzs.utils.FragmentTabUtils;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {
    private RadioGroup rgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        List<Fragment> fragmentList = new ArrayList<Fragment>();

        fragmentList.add(new GiftFragment());
        fragmentList.add(new ActiveFragment());
        fragmentList.add(new MoneyFragment());
        fragmentList.add(new MineFragment());
        rgs = (RadioGroup) this.findViewById(R.id.home_radioGroup);
        FragmentTabUtils tabUtils = new FragmentTabUtils(fragmentList, rgs, getSupportFragmentManager(), R.id.home_fragment_container);
        tabUtils.setOnRgsExtraCheckedChangedListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String submitTxt) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(),SearchActivity.class);
                intent.putExtra("key", submitTxt);
                startActivity(intent);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String arg0) {
                // TODO Auto-generated method stub
                return false;
            }
        });
        return true;
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {
    }

    /**
     * 第一次按下去的时间
     */
    private long firstTime;

    /**
     * 系统返回键监听
     */
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
            return;
        } else {
            //两次按键小于2秒时，退出应用
            android.os.Process.killProcess(android.os.Process.myPid());
        }

    }
}
